package com.pat.jrgwt.client;

import java.util.HashMap;

import com.google.gwt.core.client.GWT;
import com.pat.jrgwt.shared.FuturesContractsAmountView;
import com.pat.jrgwt.shared.FuturesTradesAmountView;

public class IndicesContractsTradesPor3View extends BaseMonthTableView
{
    protected HashMap<String, double[]> m_data = new HashMap<String, double[]>();
    protected String[] m_arMarkets = null;

    public IndicesContractsTradesPor3View()
    {
        setLabel("Indices: #Contracts*(Pips*Point Value-#Trades*Fees)/EURUSD");
    }
    
    public void setMarketsNames(String[] markets)
    {
        m_arMarkets = markets;
        // Re-define headers
        setHeaders();
    }
    
    @Override
    public void recalculate()
    {
        m_arCellValues = new double[m_arRowLabels.length][m_arHeaders.length];
        FuturesContractsAmountView futuresContracts = FuturesContractsAmountView.getInstance();
        FuturesTradesAmountView futuresTrades = FuturesTradesAmountView.getInstance();
        ProfitLossesPipsPor3 pipsView = ProfitLossesPipsPor3.getInstance();
        EuroConversion euroConversion = EuroConversion.getInstance();
        double eurUsdRatio = 0;
        double v = 0;
        double[] sum = new double[getDataRows()];
        
        for( int month = 1; month <= getDataRows(); month++ )
        {
            sum[month - 1] = 0;

            // Will be used for all formulas
            eurUsdRatio = euroConversion.getRatio("EURUSD", month);

            v = futuresContracts.getSafe(month, 1) * (pipsView.getSafe(month, 1) * 25 - futuresTrades.getSafe(month, 1) * 30);
            setAt(month, 1, v);
            sum[month - 1] += v;
            
            v = futuresContracts.getSafe(month, 2) * (pipsView.getSafe(month, 2) * 25 - futuresTrades.getSafe(month, 2) * 30);
            setAt(month, 2, v);
            sum[month - 1] += v;

            v = futuresContracts.getSafe(month, 3) * (pipsView.getSafe(month, 3) * 10 - futuresTrades.getSafe(month, 3) * 30);
            setAt(month, 3, v);
            sum[month - 1] += v;

            v = futuresContracts.getSafe(month, 4) * (pipsView.getSafe(month, 4) * 50 - futuresTrades.getSafe(month, 4) * 30);
//            ratio = euroConversion.getRatio("EURUSD", month);
            v = (eurUsdRatio == 0) ? 0 : v / eurUsdRatio;
            setAt(month, 4, v);
            sum[month - 1] += v;

            v = futuresContracts.getSafe(month, 5) * (pipsView.getSafe(month, 5) * 50 - futuresTrades.getSafe(month, 5) * 30);
//            ratio = euroConversion.getRatio("EURUSD", month);
            v = (eurUsdRatio == 0) ? 0 : v / eurUsdRatio;
            setAt(month, 5, v);
            sum[month - 1] += v;

            // NQ
            v = futuresContracts.getSafe(month, 6) * (pipsView.getSafe(month, 6) * 20 - futuresTrades.getSafe(month, 6) * 30);
//            ratio = euroConversion.getRatio("EURUSD", month);
            v = (eurUsdRatio == 0) ? 0 : v / eurUsdRatio;
            setAt(month, 6, v);
            sum[month - 1] += v;

            v = futuresContracts.getSafe(month, 7) * (pipsView.getSafe(month, 7) * 5 - futuresTrades.getSafe(month, 7) * 10);
//            ratio = euroConversion.getRatio("EURUSD", month);
            v = (eurUsdRatio == 0) ? 0 : v / eurUsdRatio;
            setAt(month, 7, v);
            sum[month - 1] += v;
            
            v = futuresContracts.getSafe(month, 8) * (pipsView.getSafe(month, 8) * 10 - futuresTrades.getSafe(month, 8) * 20);
            setAt(month, 8, v);
            sum[month - 1] += v;
            
            v = pipsView.getSafe(month, 9) * 5 * futuresContracts.getSafe(month, 9);
            v = (eurUsdRatio == 0) ? 0 : v / eurUsdRatio;
            setAt(month, 9, v);
            sum[month - 1] += v;

            v = pipsView.getSafe(month, 10) * 10 * futuresContracts.getSafe(month, 10);
            v = (eurUsdRatio == 0) ? 0 : v / eurUsdRatio;
            setAt(month, 10, v);
            sum[month - 1] += v;
        }
        
        // Set calculated sum for each month
        for( int i = 0; i < getDataRows(); i++ )
        {
            setAt(i + 1, 11, sum[i]);
        }
    }

    @Override
    protected void setHeaders()
    {
//        int total = m_arMarkets == null ? 0 : m_arMarkets.length;
//        // All exchanges name + sum
//        m_arHeaders = new String[total + 1];
//        if( total > 0 )
//        {
//            int pos = 0;
//            for( String key : m_arMarkets )
//            {
//                m_arHeaders[pos++] = key;
//            }
//        }
//        m_arHeaders[total] = "Sum";
        m_arHeaders = new String[] {"FDAX-120", "FDAX-38", "FESX", "S&P-65", "S&P-30", "NQ", "DJ", "FGBL", "EURUSD", "YPNUSD", "Sum"};
    }

    @Override
    public void onYearChanged(int year)
    {
        // TODO Auto-generated method stub

    }

}
